#include <sys/types.h>
#include <sys/socket.h>
#include <pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc,char **argv){
	if(argc!=3){
		printf("argument is too few.\n");
	}

	struct sockaddr_in addr;
	int sock;
	sock=socket(AF_INET,SOCK_DGRAM,0);
	if(sock<0){
		perror("socket");
		exit(1);
	}
	addr.sin_family=AF_INET;
	addr.sin_port=htons(atoi(argv[2]));
	addr.sin_addr.s_addr=inet_addr(argv[1]);
	if(addr.sin_addr.s_addr==INADDR_NONE){
		printf("no ip\n");
		close(sock);
		exit(1);
	}
	char buff[512];
	strcpy(buff,"hello");
	int len=sizeof(addr);
	while(1){
		int n;
		n=sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&addr,sizeof(addr));
		if(n<0){
			perror("sendto");
			close(sock);
			break;
		}
		n=recvfrom(sock,buff,512,0,(struct sockaddr *)&addr,&len);
		if(n>0){
			buff[n]=0;
			printf("received:\n");
			puts(buff);
		}
		else if(n==0){
			printf("server closed\n");
			close(sock);
			break;
		}
		else if(n==-1){
			perror("recvfrom");
			close(sock);
			break;
		}
	}
	return 0;
}
